<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="施工单位名称" prop="buildName" label-width="100px">
        <el-input v-model="queryParams.buildName" placeholder="请输入名称/联系人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['basic:unit:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['basic:unit:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['basic:unit:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['basic:unit:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="unitList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" width="68" />
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="施工单位编号" align="center" prop="buildId" /> -->
      <el-table-column label="施工单位名称" align="center" prop="buildName" />
      <el-table-column label="联系电话" align="center" prop="contactTel" />
      <el-table-column label="国税号" align="center" prop="nationaltaxNumber" />
      <el-table-column label="通讯地址" align="center" prop="mailingAddress" />
      <el-table-column label="开户行" align="center" prop="bank" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="email地址" align="center" prop="email" />
      <el-table-column label="银行账号" align="center" prop="bankAccount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:unit:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['basic:unit:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改施工单位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-row :gutter="10">
          <el-col :span="12" class="infoRow">
            <el-form-item label="名称" prop="buildName">
              <el-input v-model="form.buildName" placeholder="请输入施工单位名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12" class="infoRow">
            <el-form-item label="联系人" prop="contactPerson">
              <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12" class="infoRow">
            <el-form-item label="联系电话" prop="contactTel">
              <el-input v-model="form.contactTel" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12" class="infoRow">
            <el-form-item label="国税号" prop="nationaltaxNumber">
              <el-input v-model="form.nationaltaxNumber" placeholder="请输入国税号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12" class="infoRow">
            <el-form-item label="详细地址" prop="mailingAddress">
              <el-input v-model="form.mailingAddress" placeholder="请输入详细地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12" class="infoRow">
            <el-form-item label="传真" prop="fax">
              <el-input v-model="form.fax" placeholder="请输入传真" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12" class="infoRow">
            <el-form-item label="职务" prop="position">
              <el-input v-model="form.position" placeholder="请输入职务" />
            </el-form-item>
          </el-col>
          <el-col :span="12" class="infoRow">
            <el-form-item label="email地址" prop="email">
              <el-input v-model="form.email" placeholder="请输入email地址" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12" class="infoRow">
            <el-form-item label="开户银行" prop="bank">
              <el-select v-model="form.bank" filterable placeholder="请选择开户银行">
                <el-option v-for="bank in banks" :key="bank.value" :label="bank.text" :value="bank.text"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" class="infoRow">
            <el-form-item label="银行账号" prop="bankAccount">
              <el-input v-model="form.bankAccount" placeholder="请输入银行账号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="24" class="infoRow">
            <el-form-item label="单位网址" prop="unitUrl">
              <el-input v-model="form.unitUrl" placeholder="请输入单位网址" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUnit, getUnit, delUnit, addUnit, updateUnit } from "@/api/basic/unit";
import bank from "@/api/basic/bank";

export default {
  name: "Unit",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 施工单位表格数据
      unitList: [],
      banks: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        buildName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        buildName: [
          { required: true, message: "施工单位名称不能为空", trigger: "blur" }
        ],
        mailingAddress: [
          { required: true, message: "通讯地址不能为空", trigger: "blur" }
        ],
        contactPerson: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        editedBy: [
          { required: true, message: "最近一次修改人不能为空", trigger: "blur" }
        ],
        editedTime: [
          { required: true, message: "最近一次修改时间不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "启用状态不能为空", trigger: "change" }
        ],
        isDeleted: [
          { required: true, message: "删除状态不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询施工单位列表 */
    getList() {
      this.banks = bank.list;
      this.loading = true;
      listUnit(this.queryParams).then(response => {
        this.unitList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        buildId: null,
        buildName: null,
        contactTel: null,
        nationaltaxNumber: null,
        mailingAddress: null,
        fax: null,
        bank: null,
        contactPerson: null,
        email: null,
        bankAccount: null,
        position: null,
        unitUrl: null,
        createBy: null,
        createTime: null,
        editedBy: null,
        editedTime: null,
        status: null,
        isDeleted: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.buildId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加施工单位";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const buildId = row.buildId || this.ids
      getUnit(buildId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改施工单位";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.buildId != null) {
            updateUnit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUnit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const buildIds = row.buildId || this.ids;
      this.$modal.confirm('是否确认删除施工单位编号为"' + buildIds + '"的数据项？').then(function () {
        return delUnit(buildIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basic/unit/export', {
        ...this.queryParams
      }, `unit_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.infoRow {
  margin-bottom: 10px;
}
</style>
